/**
 * Digitalus Framework
 * 
 * LICENSE
 * 
 * This source file is subject to the new BSD license that is bundled with this
 * package in the file LICENSE.txt. If you did not receive a copy of the license
 * and are unable to obtain it through the world-wide-web, please send an email
 * to info@digitalusmedia.net so we can send you a copy immediately.
 * 
 * @category Digitalus Framework
 * @package Dig_Jquery
 * @copyright Copyright (c) 2009, Digitalus Media USA (digitalusmedia.net)
 * @license New BSD License
 * @author: Forrest Lyman
 * 
 */

/**
 * the findRelated method makes it easier to specify relative items
 * for example, you may have 10 blocks that all have edit links that 
 * have the edit class. with the related function you are able to 
 * specify other items in the same block without managing unique ids
 * and their relationships
 * 
 * @param parentSelector, the selector for the item which wraps your block
 * @param itemSelector, the selector for the items that you are attempting to select
 */
jQuery.fn.findRelated = function(parentSelector, itemSelector) {
    var parent = $(this).closest(parentSelector);
    if(parent) {
        return parent.find(itemSelector);
    }
}

jQuery.digitalusApi = {
    /**
     * the request method runs an API request
     * 
     * @param module, the module that contains the api
     * @param api, the api (which maps to a model class in the module)
     * @param method, the method to run. all of the public static methods in the model are exposed
     * @param params, a json object which contains the arguments for the method
     * @param success, a callback function to run on success
     * @param failure, a callback function to run on failure
     * 
     * @example $.digitalusApi.request('navigation', 'menu', 'addItem', {parentId: 2});
     */
    request : function(module, service, method, params, success, failure) {
        $.post("/dig/service/json/digModule/" + module + "/service/"+ service + "/method/"
                + method, params, function(response) {
            if (response.success == true) {
                if ($.isFunction(success)) {
                    success(response);
                }
            } else {
                if ($.isFunction(failure)) {
                    failure(response);
                } else {
                    alert('Sorry, an error occurred.');
                }
            }
        }, "json");
    },
    /**
     * the view method enables jquery to render a zend view
     * 
     * @param module, the module that contains the view
     * @param viewScript, the path to the view script (relative to the module's views/scripts folder
     * @param viewParams, a json object which contains any data to pass to the view
     * @param success, a callback function to run on success
     * @param failure, a callback function to run on failure
     */
    view : function(module, viewScript, viewParams, success, failure) {
        viewParams.mod = module;
        viewParams.view_script = viewScript;
        $.post("/dig/service/view", viewParams, function(response) {
            if (response) {
                if ($.isFunction(success)) {
                    success(response);
                }
            } else {
                if ($.isFunction(failure)) {
                    failure(response);
                } else {
                    alert('Sorry, an error occurred.');
                }
            }
        });
    }
}
